﻿namespace StackDeck.ViewModels
{
    using System.ComponentModel.Composition;

    using Caliburn.Micro;
    
    public class ShellViewModel : Conductor<IColumn>.Collection.OneActive, IShellViewModel
    {
        private readonly IColumnFactory columnFactory;        

        private IColumn selectedColumn;

        private const int MaxColumns = 5;

        [ImportingConstructor]
        public ShellViewModel(IColumnFactory columnFactory)
        {
            this.columnFactory = columnFactory;

            this.DisplayName = "StackDeck";

            this.Items.CollectionChanged += this.ItemsCollectionChanged;
        }

        private void ItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            this.NotifyOfPropertyChange(() => CanNewColumn);
        }

        public IColumn SelectedColumn
        {
            get
            {
                return this.selectedColumn;
            }

            set
            {
                this.selectedColumn = value;
                this.NotifyOfPropertyChange(() => this.SelectedColumn);
            }
        }

        public bool CanNewColumn
        {
            get
            {
                return this.Items.Count < MaxColumns;
            }
        }

        public void NewColumn()
        {
            if (this.CanNewColumn)
            {
                this.ActivateItem(columnFactory.NewCoreColumn());                
            }
        }

        public void SelectColumn(IColumn column)
        {
            this.SelectedColumn = column;
        }
    }
}